import { baseURL } from './env.js';
import { envType } from './env.js';
export default {
	data () {
		return {
			IMGURL:'https://jbk.maoyishiji.com/static/img',
			SwiperAutoTime:3000,
			indicatorActiveColor:"#FFDD40",
			indicatorColor:'#DDDDDD',
			moreStatus:"nomore",
			total:0,
			checkUserInfo:{},
			fixNum:uni.getStorageSync('fixNum') || '100'
		}
	},
	//分享小程序
	onShareAppMessage(){
		
	},
	// 分享到朋友圈
	onShareTimeline(){
		
	},
	getMoreStatus(){
		if(this.list.length<this.total){
			this.moreStatus = 'more'
		}else{
			this.moreStatus = 'nomore'
		}
	},
		
	mounted(){
		
	},
	methods: {
		chooseImageH5(count=1){
			return new Promise((resolve, reject) => {
				this.$wx.chooseImage({
					count,
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: res1 => {
						uni.showLoading({
							title: '上传中...'
						})
						const localId = res1.localIds[0];
						this.$wx.uploadImage({
						  localId: localId, // 需要上传的图片的本地ID，由chooseImage接口获得
						  isShowProgressTips: 0, // 默认为1，显示进度提示
						  success:res2 =>{
							  uni.showLoading({
							  	title: '上传中...'
							  })
							  var serverId = res2.serverId; // 返回图片的服务器端ID
							  let params = {
								  mediaId:serverId,
								  isLoading:0
							  }
							  this.$api.base.uplodeServerId(params).then(res3=>{
								  uni.hideLoading()
								  resolve(res3)
							  })
							  
						  }
						});
						
					},
					fail: err => {
						uni.hideLoading()
						reject(err)
					}
				})
			}).catch(error => {
				console.error(error)
			})
			
		},
		// 复制
		copy(val){
			if(!val) return
			uni.setClipboardData({
				data: val,
			});
		},
		// 获取第三方黄金数据 
		getPriceLoop(goods_type_name,type=1){
			if(!goods_type_name) return 
			
			return new Promise((resolve, reject) => {
				
				this.$api.base.getGoldLoopData({isLoading:0,goods_type_name:goods_type_name}).then(res=>{
					let gold_price = res.data.gold_price
					let arr = []
					for(let i in res.data.items){
						arr.push(res.data.items[i])
					}
					
					let filterData = arr.filter((el,i)=>{
						return el.code == gold_price.goods_code
					})
					let dd = filterData[0]
					let re_price = dd.bidprice*1+gold_price.float_re_price*1
					let sael_price = dd.askprice*1+gold_price.float_sael_price*1
					let obj = {
						re_price:Math.floor(re_price*this.fixNum)/this.fixNum, // 回购价格
						sael_price:Math.floor(sael_price*this.fixNum)/this.fixNum    // 销售价格
					}
					resolve(obj)
					
				})
				
			})
			
			
		},
		toPathNeedLogin(path,type=1){ // 1需要登录且认证 2需要登录不需要认证 3不需要
			if(path){
				if(type==1 || type==2){
				  this.checkLogin(type).then(res=>{
						if(res){
							this.$utils.jump(path)
						}
				  })
				}else{
					this.$utils.jump(path)
				}
			}else{
				this.$utils.Toast('暂未开放')
			}
		},
		checkLogin(type){
			let mid = uni.getStorageSync('mid')
			if(!mid){
				uni.showModal({
					title:'请授权登录',
					success:(e)=>{
						if(e.confirm){
							uni.navigateTo({
								url:'/pages/login/login'
							})
						}
					}
				})
				return false
			}
			
			return new Promise((resolve, reject) => {
				this.$api.user.lodeUserInfo().then(res=>{
					let bool = true
					if(!res.data){
						uni.showModal({
							title:'请授权登录',
							success:(e)=>{
								if(e.confirm){
									uni.navigateTo({
										url:'/pages/login/login'
									})
								}
							}
						})
						bool = false
					}else{
						if(res.data.is_renzheng==0 && type==1){
							uni.showModal({
								title:'请实名认证',
								success:(e)=>{
									if(e.confirm){
										uni.navigateTo({
											url:'/pages/renzheng/renzheng'
										})
									}
								}
							})
							bool = false
						}
						if(res.data.is_renzheng==1 && type==1){
							uni.showModal({
								title:'请签署协议',
								success:(e)=>{
									if(e.confirm){
										uni.navigateTo({
											url:'/pages/editInfo/editInfo'
										})
									}
								}
							})
							bool = false
						}
					}
					resolve(bool)
					
				})
			})
			
			
			
		},
		getMoreStatus(){
			if(this.list.length<this.total){
				this.moreStatus = 'more'
			}else{
				this.moreStatus = 'nomore'
			}
		},
		getDate(type) {
			const date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();
			
			if (type === 'start') {
				year = year - 60;
			} else if (type === 'end') {
				year = year + 2;
			}
			// if (type === 'start') {
			// 	year = year - 1;
			// } else if (type === 'end') {
			// 	year = year;
			// }
			month = month > 9 ? month : '0' + month;
			day = day > 9 ? day : '0' + day;
			return `${year}-${month}-${day}`;
		},
		// 获取微信配置
		getWxConfig(){
			if(envType=='dev'){
				this.$utils.Toast('本地环境')
				return false
			}
			let token = uni.getStorageSync('token')
			let mid = uni.getStorageSync('mid')
			return new Promise((resolve, reject) => {
				let params = {
					token:token,
					mid: mid,
					webUrl:location.href.split('#')[0]
				}
				this.$api.base.getWxConfig(params).then(res=>{
					let result = res.data
					this.$wx.config({
					   // 开启调试模式,
					   debug: false,
					   // 必填，企业号的唯一标识，此处填写企业号corpid
					   appId: result.appId,
					   // 必填，生成签名的时间戳
					   timestamp: result.timestamp,
					   // 必填，生成签名的随机串
					   nonceStr: result.nonceStr,
					   // 必填，签名，见附录1chooseImage
					   signature: result.signature,
					   // 必填，需要使用的JS接口列表，所有JS接口列表见附录2   'chooseImage','uploadImage'
					   jsApiList: ['chooseImage','translateVoice' ,'chooseCard', 'addCard', 'openCard', 'scanQRCode', 'updateAppMessageShareData',
					   'onMenuShareTimeline', 'onMenuShareAppMessage', 'updateTimelineShareData', 'uploadImage', 'previewImage',
						'getLocalImgData', 'downloadImage', 'startRecord', 'stopRecord', 'uploadVoice', 'playVoice', 'downloadVoice',
						'getLocation', 'openAddress', 'getJsConfig', 'chooseWXPay']
					})
					resolve(res)
				})
			})
			
			
			
			
			
		}
	}
}
